<?php

namespace falcon5\inc\Infrastructure\Services\Falcon5;

use falcon5\inc\Exceptions\RemoteServiceException;
use lucatume\DI52\ContainerException;

trait EntryTrait
{
    /**
     * @param array $body
     * @return mixed
     * @throws ContainerException
     * @throws RemoteServiceException
     */
    public function saveEntry(array $body)
    {
        $response = $this->getClientEngine()->saveEntry($body);

        $content = $response->getBody()->getContents();

        if ($response->getStatusCode() != 200) {
            throw new RemoteServiceException($content, $response->getStatusCode());
        }

        return json_decode($content, true);
    }

    /**
     * @param int $pageId
     * @param string $originalLang
     * @param string $translatedLang
     * @param string $keyword
     * @param int $page
     * @param int $pageSize
     * @return mixed
     * @throws ContainerException
     * @throws RemoteServiceException
     */
    public function entryList(int $pageId, string $originalLang, string $translatedLang, string $keyword, int $page = 1, int $pageSize = 10)
    {
        $response = $this->getClientEngine()->entryList($pageId, $originalLang, $translatedLang, $keyword, $page, $pageSize);

        $content = $response->getBody()->getContents();

        if ($response->getStatusCode() != 200) {
            throw new RemoteServiceException($content, $response->getStatusCode());
        }

        return json_decode($content, true);
    }
}